﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DeltaFlex.Negoci;

using Negoci = DeltaFlex.Negoci;

namespace Vista.Controls
{
    public partial class AltaClientArticlePreu : System.Web.UI.UserControl
    {
        #region Variables Globals
        public int IdClient
        {
            get
            {
                return (Convert.ToInt32(ViewState["_idClient"]) == 0 ?
                   0 : Convert.ToInt32(ViewState["_idClient"]));
            }
            set { ViewState["_idClient"] = value; }
        }

        public int IdArticle
        {
            get
            {
                return (Convert.ToInt32(ViewState["_idArticle"]) == 0 ?
                    0 : Convert.ToInt32(ViewState["_idArticle"]));
            }
            set { ViewState["_idArticle"] = value; }
        }

        public decimal PVP
        {
            get
            {
                return (Convert.ToDecimal(ViewState["_pvp"]) == 0 ?
                   0 : Convert.ToDecimal(ViewState["_pvp"]));
            }
            set { ViewState["_pvp"] = value; }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region Carrega a memoria
        /// <summary>
        /// Carrega el contingut dels text Box del control Alta Màquina
        /// </summary>
        private void CarregarMemoria()
        {
            IdClient = Convert.ToInt32(ddlClient.SelectedValue);
            IdArticle = Convert.ToInt32(ddlArticle.SelectedValue);
            PVP = txtPreu.Text == string.Empty ? 0 : Convert.ToDecimal(txtPreu.Text);
        }
        #endregion

        #region Neteja Controls
        /// <summary>
        ///Neteja tots els controls del formulari.
        /// </summary>
        /// <param name="controles"></param>
        private void NetejaControls(ControlCollection controles)
        {
            foreach (Control control in controles)
            {
                if (control is TextBox)
                    ((TextBox)control).Text = string.Empty;
                else if (control is DropDownList)
                    ((DropDownList)control).ClearSelection();
                else if (control is RadioButtonList)
                    ((RadioButtonList)control).ClearSelection();
                else if (control is CheckBoxList)
                    ((CheckBoxList)control).ClearSelection();
                else if (control is RadioButton)
                    ((RadioButton)control).Checked = false;
                else if (control is CheckBox)
                    ((CheckBox)control).Checked = false;
                else if (control.HasControls())
                    //Esta linea detecta un Control que contigui altres Controls
                    //Així cap control es quedarà sense netejar
                    NetejaControls(control.Controls);
            }
        }
        #endregion

        #region Guardar
        protected void btnAltaRelacio_Click(object sender, EventArgs e)
        {
            string missatge, exepcio;
            missatge = exepcio = string.Empty;

            CarregarMemoria();

            try
            {
                Negoci.ClientArticlePreu.InsertarClientArticlePreu(IdClient, IdArticle, PVP);
                
            }

            catch (Exception ex)
            {
                exepcio = "Error en donar d'alta un nou client <br/> " + ex.Message;
            }

            if (String.IsNullOrEmpty(exepcio))
                missatge = ConstantsAltes.mplbGuardarRelacioOK;
            else
                missatge = ConstantsAltes.mplbGuardarRelacioKO + " <br/> " + exepcio;

            mpAltaClientArticlePreu.LabelHeader = ConstantsAltes.mplhGuardarRelacio;
            mpAltaClientArticlePreu.LabelBody = missatge;
            mpAltaClientArticlePreu.MostraInfo = true;

            NetejaControls(this.Controls);

            mpAltaClientArticlePreu.MostrarModal();
        }
        #endregion
    }
}